home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Tree / Binary_Node.C < prev    next >
C/C++ Source or Header  |  1992-05-15  |  3KB  |  88 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/Binary_Node.h>
  14.  
  15. // CoolBinary_Node -- Simple constructor to initialize a node object
  16. // Input:         None
  17. // Output:        None
  18.  
  19. template <class Type>
  20. CoolBinary_Node<Type>::CoolBinary_Node<Type> () {
  21. }
  22.  
  23.  
  24. // CoolBinary_Node -- constructor to initialize a node object and assign a value
  25. //                to the data slot
  26. // Input:         Data slot value
  27. // Output:        None
  28.  
  29. template <class Type>
  30. CoolBinary_Node<Type>::CoolBinary_Node<Type> (const Type& value) {
  31.   this->data = value;                // Assign data value
  32. }
  33.  
  34. // CoolBinary_Node -- Copy constructor to copy node and all subnodes
  35. // Input:         Binary node reference
  36. // Output:        Binary node reference
  37.  
  38. template <class Type>
  39. CoolBinary_Node<Type>::CoolBinary_Node<Type> (const CoolBinary_Node<Type>& bn) {
  40.   this->ltree = this->copy_nodes(bn.get_ltree()); // copy left tree 
  41.   this->rtree = this->copy_nodes(bn.get_rtree()); // copy right tree
  42.   this->avl_balance = bn.avl_balance;          // copy avl balance
  43.   this->data = bn.data;                  // copy data at this node
  44. }
  45.  
  46.  
  47. // ~CoolBinary_Node -- Destructor must be virtual
  48. // Input:          None
  49. // Output:         None
  50.  
  51. template <class Type>
  52. CoolBinary_Node<Type>::~CoolBinary_Node<Type> () {} // data deleted when is object
  53.  
  54.  
  55.  
  56. // operator= -- Assignment with another binary node, with recursive deep copy.
  57. // Input:       reference to node
  58. // Ouput:       mutated *this
  59.  
  60. template <class Type>
  61. CoolBinary_Node<Type>& CoolBinary_Node<Type>::operator= (const CoolBinary_Node<Type>& n) {
  62.   delete this->ltree;                // free old subnodes
  63.   delete this->rtree;
  64.   this->ltree = copy_nodes (n.get_ltree());    // Copy the left tree
  65.   this->rtree = copy_nodes (n.get_rtree());    // Copy the right tree
  66.   this->avl_balance = n.avl_balance;        // Copy avl balance
  67.   this->data = n.data;                // Copy value at node
  68.   return *this;
  69. }
  70.  
  71.  
  72. // copy_nodes -- Copies this node and all its subnodes
  73. // Input:       pointer to node to be copied
  74. // Output:      pointer to new copy of node with all new subnodes.
  75.  
  76. template <class Type>
  77. CoolBinary_Node<Type>* CoolBinary_Node<Type>::copy_nodes (const CoolBinary_Node<Type>* n) const {
  78.   if (n == NULL)                
  79.     return NULL;
  80.   CoolBinary_Node<Type>* new_n = new CoolBinary_Node<Type>; // Allocate a new node
  81.   new_n->ltree = copy_nodes (n->get_ltree());            // Copy the left tree
  82.   new_n->rtree = copy_nodes (n->get_rtree());            // Copy the right tree
  83.   new_n->data = n->data;                    // Copy the value
  84.   new_n->avl_balance = n->avl_balance;                // Copy avl balance
  85.   return new_n;                            // Return copied node.
  86. }
  87.  
  88.